﻿using System;
using System.Collections.Generic;
using System.Text;
using Semantics;
using Semantics.Base;
namespace Semantics.Parse
{
    public class AmbiguityResolver
    {
        public class LangNodeAmbiguities
        {
            public LangNodeAmbiguities(LangNode langnode)
            {
                this.LangNode = langnode;
            }
            public LangNode LangNode;
            public SortedList<int, LangNode> Parents = new SortedList<int, LangNode>();  
            public override string ToString()
            {
                string text = this.LangNode + "(";
                for (int i = 0; i < Parents.Values.Count; i++)
                    text+=Parents.Values[i]+";";
                text += ")";
                return text;
            }
        }
        public class CorrectNode
        {
            
            public string value;
            public bool IsScanned;            
        }
        SortedList<int, LangNodeAmbiguities> dictionary = new SortedList<int, LangNodeAmbiguities>();
        SortedList<int, string> leafNodelist;
        SortedList<int, LangNodeAmbiguities> ambiguitieslist = new SortedList<int, LangNodeAmbiguities>();
        public SortedList<int, LangNodeAmbiguities> Resolve(SortedList<int, string> leafNodelist,List<SemanticNode> semanticnodes)
        {
            this.leafNodelist = leafNodelist;
            SortedList<string, string> leafNode = new SortedList<string, string>();
            for (int i = 0; i < semanticnodes.Count; i++)
            {
                Hash(leafNode,semanticnodes[i], null);
            }
                        
            IList<LangNodeAmbiguities> ambiguities=dictionary.Values;
            for (int i = 0; i < ambiguities.Count; i++)
            {
                LangNodeAmbiguities ambiguity = ambiguities[i];
                if(!ambiguitieslist.ContainsKey(ambiguity.LangNode.Position.Key))
                    ambiguitieslist.Add(ambiguity.LangNode.Position.Key, ambiguity);
                if (leafNode.ContainsKey(ambiguity.LangNode.Value))
                    leafNodelist.Add(ambiguity.LangNode.Position.Key, ambiguity.LangNode.Value);                 
            }
           
            return ambiguitieslist;
        }

        void Hash(SortedList<string, string> leafNode, SemanticNode semanticnode, SemanticNode parent)
        {
            if (parent != null)
            {
                LangNodeAmbiguities ambiguities = GetLangNodeAmbiguities(semanticnode.LanguageNode);
                if(!ambiguities.Parents.ContainsKey(parent.LanguageNode.SN))
                    ambiguities.Parents.Add(parent.LanguageNode.SN, parent.LanguageNode);
            }
            if(semanticnode.Children.Count==0)
                if (!leafNode.ContainsKey(semanticnode.Value))
                    leafNode.Add(semanticnode.Value, semanticnode.Value);
            for (int i = 0; i < semanticnode.Children.Count; i++)
            {
                Hash(leafNode,semanticnode.Children[i].Child, semanticnode);
            }
        }

        LangNodeAmbiguities GetLangNodeAmbiguities(LangNode langnode)
        {
            LangNodeAmbiguities ambiguities = null;
            if (dictionary.ContainsKey(langnode.SN))
                ambiguities = dictionary[langnode.SN];
            else
            {
                ambiguities = new LangNodeAmbiguities(langnode);
                dictionary.Add(langnode.SN, ambiguities);
            }
            return ambiguities;
        }
        SortedList<int, CorrectNode> CorrectNodeList=new SortedList<int,CorrectNode>();

        public int findIndexInAmbiguitieslist(string key)
        {
            foreach (KeyValuePair<int, LangNodeAmbiguities> item in ambiguitieslist)
            {
                if (string.Compare(key, item.Value.LangNode.Value) == 0)
                    return item.Key;
            }
            return -1;
        }
        //初始化正确结点域，即将所有的叶子结点放入正确结点域中
        public SortedList<int,CorrectNode>  InitialCorrectNode()
        {
            //CorrectNode newCorrectNode=new CorrectNode();
            
          
            foreach (KeyValuePair<int, string> item in this.leafNodelist)
            {
                addInitialCorrectNode(item.Key, item.Value);             

            }            
            //this.CorrectNodeList = this.leafNodelist;
            return CorrectNodeList;
        }
        void addInitialCorrectNode(int key,string value)
        {
            CorrectNode tempNode = new CorrectNode();
            tempNode.value =value;
            tempNode.IsScanned = false;


            CorrectNodeList.Add(key, tempNode);
        }
        //被选出来的结点，加入正确节点域中
        public SortedList<int, CorrectNode> AddCorrectNode(LangNodeAmbiguities Node, int key, SortedList<int, CorrectNode> CorrectNodeList)
        {
            if (!CorrectNodeList.ContainsKey(key))
            {
                CorrectNode newCorrectNode = new CorrectNode();
                newCorrectNode.value = Node.LangNode.Value;
                newCorrectNode.IsScanned = false;
                CorrectNodeList.Add(key, newCorrectNode);                
            }
            return CorrectNodeList;
        }
        public SortedList<int, CorrectNode> AddLangNodeToCorrectNode(LangNode Node, SortedList<int, CorrectNode> CorrectNodeList)
        {
            if (!CorrectNodeList.ContainsKey(Node.Position.Key))
            {
                CorrectNode newCorrectNode = new CorrectNode();
                newCorrectNode.value = Node.Value;
                newCorrectNode.IsScanned = false;
                CorrectNodeList.Add(Node.Position.Key, newCorrectNode);
            }
            return CorrectNodeList;
        }
        public bool IsInCorrectNode(int key)
        {
           foreach (KeyValuePair<int, CorrectNode> item in CorrectNodeList)
           {
               if (item.Key == key)
                   return true;
           }
           return false;
        }

        //public class interactiveNode
        //{
        //    public int key;
        //    public SortedList<int, LangNode> Parents = new SortedList<int, LangNode>(); 
        //}
        public class interactNode 
        {
            public LangNode LangNode;
            public SortedList<int, LangNode> Parents=new SortedList<int,LangNode>();  
        }

        public class resultOfCount 
        {
            public int count;
            public interactNode interactiveNode=new interactNode();

        }

        public resultOfCount AmbiguitiesParentAllInCorrectNode(int key, SortedList<int, LangNodeAmbiguities> ambiguitieslist, SortedList<int, Semantics.Parse.AmbiguityResolver.CorrectNode> CorrectNodeList)
        {
            int count = 0;
            int i;
            resultOfCount result=new resultOfCount();
            bool isInCorrectNode;
            int eliminateCount=0;
            List<int> eliminateParentKey=new List<int>();
            this.CorrectNodeList = CorrectNodeList;
            this.ambiguitieslist = ambiguitieslist;
            foreach (KeyValuePair<int, LangNode> item in ambiguitieslist[key].Parents)
            {
                
                for (i = 0; i < item.Value.Children.Count; i++)
                {
                    isInCorrectNode = IsInCorrectNode(item.Value.Children[i].Position.Key);
                    if (!isInCorrectNode)
                        break;
                    if (isInCorrectNode)
                        if(CorrectNodeList[item.Value.Children[i].Position.Key].IsScanned == true)
                        {
                            eliminateParentKey.Add(item.Key);
                            eliminateCount=eliminateCount+1;
                            break;
                        }
                    if (i == item.Value.Children.Count - 1 && isInCorrectNode)
                    {
                        //针对key这个结点，构造一个数据结构，这个数据结构包含需要进行交互的歧异，
                        result.interactiveNode.LangNode = ambiguitieslist[key].LangNode;
                        result.interactiveNode.Parents.Add(item.Key,item.Value);
                        count = count + 1;
                    }
                }
            }
            //eliminateCount是根据前面的选择排除的歧义，如果总数减去排除的歧义数量=1，则说明得到了唯一的解释方式
            //则将该结点及正确父节点的其他孩子结点都加入CorrecNOdeLIst，并标记IsScanned=true;
            LangNode tempLangNode;
            if(ambiguitieslist[key].Parents.Count==eliminateCount+1)
            {
                foreach (KeyValuePair<int, LangNode> item in ambiguitieslist[key].Parents)
                {
                    if (!eliminateParentKey.Contains(item.Key))                   
                    {
                        int ParentKey = findIndexInAmbiguitieslist(item.Value.Value);
                        CorrectNodeList = AddCorrectNode(ambiguitieslist[ParentKey], ParentKey, CorrectNodeList);
                        //CorrectNodeList[ParentKey].IsScanned = true;
                        for (i = 0; i < item.Value.Children.Count; i++)
                        {
                            tempLangNode = item.Value.Children[i];
                            int ChildKey = findIndexInAmbiguitieslist(tempLangNode.Value);
                            CorrectNodeList =AddCorrectNode(ambiguitieslist[ChildKey], ChildKey, CorrectNodeList);
                            CorrectNodeList[ChildKey].IsScanned = true;
                        }
                    }
                }
            }
            result.count = count;
            return result;
        }
    }
}
